package cn.hongwei.nacos.handler.instance;

import cn.hongwei.nacos.annotation.Handler;
import cn.hongwei.nacos.constant.RequestConstants;
import cn.hongwei.nacos.context.InstanceContext;
import cn.hongwei.nacos.dto.InstanceListDetailDto;
import cn.hongwei.nacos.handler.base.InstanceBaseHandler;
import cn.hongwei.nacos.param.NacosInfoParam;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Great
 * @email 1224028994@qq.com
 * 实力列表
 */
@Handler
public class InstanceListDetailHandler extends InstanceBaseHandler {
    //每次请求都需要 名称空间、组名、服务名
    @Override
    public void doHandler(InstanceContext instanceContext) {
        //获取请求参数
        NacosInfoParam nacosInfoParam = instanceContext.getInstanceRequest().getNacosInfoParam();
        Map<String, Map<String, List<String>>> instancesParam = instanceContext.getInstanceRequest().getInstancesParam();
        Set<String> namespaces = instancesParam.keySet();

        //所有实力详情
        List<InstanceListDetailDto> instanceResult = new ArrayList<>();
        for (String namespace : namespaces) {
            Map<String, List<String>> groupAndName = instancesParam.get(namespace);
            Set<String> groups = groupAndName.keySet();
            for (String group : groups) {
                //当前组的服务名
                List<String> serviceNames = groupAndName.get(group);
                for (String serviceName : serviceNames) {
                    try {
                        //uri /nacos/v1/ns/instance/list
                        Request request = new Request.Builder()
                                .get()
                                .url(RequestConstants.PROTOCOL_PREFIX + nacosInfoParam.getIp() + RequestConstants.MARK_COLON + nacosInfoParam.getPort() +
                                        RequestConstants.INSTANCE_PATH + RequestConstants.MARK_QUESTION +
                                        RequestConstants.NAMESPACE_ID + namespace +//namespaceId
                                        RequestConstants.REQUEST_SUFFIX_PARAM + RequestConstants.GROUP_NAME + group + //groupName
                                        RequestConstants.REQUEST_SUFFIX_PARAM + RequestConstants.SERVICE_NAME + serviceName) //serviceName
                                .build();
                        Call call = okHttpClient.newCall(request);
                        Response response = call.execute();
                        ResponseBody body = response.body();
                        String string = body.string();
                        InstanceListDetailDto instanceListDetailDto = objectMapper.readValue(string, InstanceListDetailDto.class);
                        instanceResult.add(instanceListDetailDto);
                    }catch (Exception e){
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        //将结果集设置到上下文响应体中
        instanceContext.getInstanceResponse().setInstanceListDetailDtos(instanceResult);

        doNextHandler(instanceContext);
    }
}
